<?php
/*
 *      ostable.php
 *      
 *      Copyright 2011 ViRak <vetalrakitin@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/oscomponent.php");

class ostable extends oscomponent {
	
	//public $caption;
	//public $accesskey;
	private $headers = array();
	private $items = array();
	//private $selectitem;
	private $selected;
	private $isedit = false;
	private $rows = array();
	
	function __construct($parent) {
		parent::__construct($parent);
	}
	
	function addheader($name, $caption) {
		$this->headers[$name] = array("caption" => $caption, "value" =>"");
/*
		$this->headers[$name] = new osheader($this->uuid);
		$this->headers[$name]->name = $name;
		$this->headers[$name]->caption = $caption;
*/
	}
	
	function insertitem() {
		//foreach ($this->headers as $headerkey=>$header) {
			$this->items[] = array();
		//}
/*
		$row = new osrow($this->uuid, $this->headers);
		
		$this->rows[$row->uuid] = $row;
		$this->rows[$row->uuid]->setevent($this->uuid, "oncellinput", "oncellinput");
		$this->rows[$row->uuid]->setevent($this->uuid, "oncellkeypress", "oncellkeypress");
		$this->rows[$row->uuid]->setevent($this->uuid, "onrowfocus", "onrowfocus");
*/
/*
		if ($this->selected) { 
			$this->rows[$this->selected]->isselected = false;
		}
*/
/*
		$this->selected = $row->uuid;
*/
/*
		$this->rows[$row->uuid]->isselected = true;
*/
/*
		$this->isedit = true;
*/
		$this->update();
	}
	
/*
	function onrowfocus($uuid) {
		if ($this->selected) { 
			$this->rows[$this->selected]->isselected = false;
			$this->rows[$this->selected]->update();
		}
		$this->selected = $uuid;
		$this->rows[$uuid]->isselected = true;
		//$this->selected = $uuid;
		$this->rows[$uuid]->update();
		parent::onrowfocus($uuid);
	}
*/
	
/*
	function __get($name) {
		if (array_key_exists($name, $this->headers)) {
			if (array_key_exists($this->selected, $this->rows)) {
				return $this->rows[$this->selected]->{$name};
				//$this->update();
			}
		}
	}
*/

/*
	function header($name) {
		if (array_key_exists($name, $this->headers)) {
			return $this->headers[$name];
		}
	}
*/
	
	function celloninput($value, $position) {
		$position = unserialize($position);
		parent::oncellinput($cellname, $value);
	}
	
	function oncellkeypress($cellname, $key) {
		parent::oncellkeypress($cellname, $key);
	}
	
	function get() {
		$ret = "<table id='".$this->uuid."' style='border: solid black 1px;'>";
		foreach ($this->headers as $name=>$header) {
			$ret .= "<th id='".$name."' style='border: solid black 1px;cursor:pointer;' onclick=message('".$this->uuid."','headeronclick','$name',0);>".$header["caption"]."</th>";
		}
/*
		foreach($this->headers as $header) {
			$ret .= $header->get();
		}
*/
		
/*
		foreach($this->rows as $row) {
			$ret .= $row->get();
		}
*/
		foreach ($this->items as $itemkey=>$item) {
			$ret .= "<tr style='cursor:pointer;";
/*
			if ($uuid != $this->selected) {
				$ret .= "background-color:#FFFFFF;color:#000000;'";
			} else {
				$ret .= "background-color:#B9C7D2;color:#000000;'";
			}
*/
			//$ret .= " onclick=message('".$this->uuid."','rowonclick','".$item_name."',0); ondblclick=message('".$this->uuid."','rowondblclick','".$item_name."',0);>";
			$ret .= "'>";
			foreach ($this->headers as $headerkey=>$header) {
				$ret .= "<td style='border: solid black 1px;'>";
				//$ret .= "<textarea style='margin-top: 0px; margin-bottom: 0px; height: 16px; ' nowrap>";
				$ret .= "<input style='border:0px;margin:0px;width:100%;' size='0' type='text' value='";
				if (array_key_exists($headerkey, $item)) {
					$ret .= $item[$headerkey]."'";
				} else {
					$ret .= "'";
				}
				//$ret .= $this->active?"":" disabled=disabled";
				//$ret .= $this->readonly?" readonly":"";
				$ret .= " onkeyup=message('".$this->uuid."','cellonkeypress',evn.keyCode,'".serialize(array($itemkey,$headerkey))."');";
				$ret .= " oninput=message('".$this->uuid."','celloninput',this.value,'".serialize(array($itemkey,$headerkey))."');";
				$ret .= ">";
				//$ret .= "<div contenteditable='true'>";
				//$ret .= $data[$header_name];
				//$ret .= "</div>";
				//$ret .= "</textarea>";
				$ret .= "</td>";
			}
			$ret .= "</tr>";
		}
		$ret .= "</table>";
		//$ret .= "<pre>".print_r($this->items,1)."</pre>";
/*
		$ret = "<input id=".$this->uuid." type='button' value='".$this->caption."'";
		$ret .= $this->active?"":" disabled";
		$ret .= $this->accesskey?" accesskey='".$this->accesskey."'":"";
		$ret .= " onclick=message(this.id,'onclick',0,0)";
		$ret .= ">";
*/

		return $ret;
	}
}

class osheader extends oscomponent {
	
	private $name;
	private $caption;
	public $width;
	private $readonly = false;
	
	function __construct($parent) {
		parent::__construct($parent);
	}
	
	function __get($name) {
		switch ($name) {
			case "name":{return $this->name;break;}
			case "caption":{return $this->caption;break;}
			case "readonly":{return $this->readonly;break;}
		}
	}
	
	function __set($name, $value) {
		switch ($name) {
			case "name":{$this->name = $value;break;}
			case "caption":{$this->caption = $value;break;}
			case "readonly":{$this->readonly = $value;break;}
		}
	}
	
	function get() {
		$ret = "<th id='".$this->uuid."' style='width:";
		$ret .= $this->width?$this->width."px":"auto";
		$ret .= ";overflow:auto;border: solid black 1px;cursor:pointer;' onclick=message('".$this->uuid."','onclick','".$this->name."',0);>".$this->caption."</th>";
		return $ret;
	}
}

class osrow extends oscomponent {
	
	private $cells = array();
	private $headers;
	public $bkselcolor = "#0CF1FE";
	public $bkcolor = "#B3F4F8";
	private $isselected = false;
	
	function __construct($parent, $headers) {
		parent::__construct($parent);
		$this->headers = $headers;
		foreach ($this->headers as $header) {
			$this->cells[$header->name] = new oscell($this->uuid, $header);
/*
			$this->cells[$header->name]->setevent($this->uuid, "oncellinput", "oncellinput");
			$this->cells[$header->name]->setevent($this->uuid, "oncellkeypress", "oncellkeypress");
			$this->cells[$header->name]->setevent($this->uuid, "onfocus", "onfocus");
			$this->cells[$header->name]->setevent($this->uuid, "onblur", "onblur");
*/
		}
	}
	

/*
	function oncellinput($cellname, $value) {
		//new osmsgbox($value);
		parent::oncellinput($cellname, $value);
	}
	
	function oncellkeypress($cellname, $key) {
		parent::oncellkeypress($cellname, $key);
	}
	
	function onfocus() {
		//new osmsgbox("onfocus ".$this->uuid);
		parent::onfocus();
		parent::onrowfocus($this->uuid);
		$this->isselected = true;
		$this->modify("style", $this->style());
	}
	
	function onblur() {
		//new osmsgbox("onfocus ".$this->uuid);
		parent::onblur();
		parent::onrowblur($this->uuid);
		$this->isselected = false;
		$this->modify("style", $this->style());
	}
*/

	function __get($name) {
		if (array_key_exists($name, $this->cells)) {
			return $this->cells[$name];
		}	
	}
	
	function __set($name, $value) {
/*
		if ($name == "isselected") {
			$this->isselected = $value;
			$this->modify("style", $this->style());
			//$this->update();
		}
*/
	}
	
	function style() {
		$ret = "cursor:pointer;";
		$ret .= $this->isselected?"background: ".$this->bkselcolor.";":"background: ".$this->bkcolor.";";
		$ret .= "";
		return $ret;
	}
	
	function get() {
		$ret = "<tr id='".$this->uuid."' ";
		$ret .= " style=".$this->style();
		$ret .= ">";
		foreach ($this->cells as $key=>$cell) {
			$ret .= "<td style='border: solid black 1px;'>";
			$ret .= $cell->get();
			$ret .= "</td>";
		}
		$ret .= "</tr>";
		return $ret;
	}
}

class oscell extends oscomponent {
	
	private $value;
	private $header;
	public $bkselcolor = "#0CF1FE";
	public $bkcolor = "#546667";
	
	function __construct($parent, $header) {
		parent::__construct($parent);
		$this->header = $header;
	}
	
	function oninput($value) {
		$this->value = $value;
		parent::oninput($value);
		parent::oncellinput($this->header->name, $value);
	}
	
	function onkeypress($key) {
		parent::onkeypress($key);
		parent::oncellkeypress($this->header->name, $key);
	}
	
/*
	function onfocus() {
		//parent::onfocus();
		//new osmsgbox("onfocus");
		$this->bkcolor = "#0CF1FE";
		$this->modify("style", $this->style());
	}
	
	function onblur() {
		//parent::onblur();
		//new osmsgbox("onblur");
		$this->bkcolor = "#546667";
		$this->modify("style", $this->style());
	}
*/
	
	function __set($name, $value) {
		if ($name == "value") {
			$this->value = $value;
			//$this->update();
			$this->modify("value", $value);
		}
	}
	
	function __get($name) {
		if ($name == "value") {
			return $this->value;
		}
	}
	
	function style() {
		$ret = "border:0px;margin:0px;width:100%;background:".$this->bkcolor;
		return $ret;
	}
	
	function get() {
		$ret = "<input id='".$this->uuid."' style=".$this->style()." size='0' type='text' value='".$this->value."'";
		$ret .= $this->header->readonly?" readonly":"";
		$ret .= " oninput=message(this.id,'oninput',this.value,0);";
		$ret .= " onkeypress=message(this.id,'onkeypress',evt.keyCode,0);";
/*
		$ret .= " onblur=message(this.id,'onblur',0,0);";
		$ret .= " onfocus=message(this.id,'onfocus',0,0);";
*/
		$ret .= ">";
		return $ret;
	}
}
?>
